package com.example.mall.coupon.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mall.common.utils.PageUtils;
import com.example.mall.common.utils.Query;
import com.example.mall.coupon.dao.CouponDao;
import com.example.mall.coupon.entity.CouponEntity;
import com.example.mall.coupon.service.CouponService;
import io.seata.core.context.RootContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Map;


@Service("couponService")
@Slf4j
public class CouponServiceImpl extends ServiceImpl<CouponDao, CouponEntity> implements CouponService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CouponEntity> page = this.page(
                new Query<CouponEntity>().getPage(params),
                new QueryWrapper<CouponEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public int insertCoupon(CouponEntity couponEntity) {
        log.info("=========== insertCoupon 开始 ===========");
        log.info("=========== insertCoupon 开始 ===========" + RootContext.getXID());
        int result = this.baseMapper.insert(couponEntity);
        if (result != 1) {
            log.info("插入失败！[{}]", JSON.toJSONString(couponEntity));
            throw new RuntimeException("插入记录[" + couponEntity.getId() + "]失败！");
        }
        log.info("=========== insertCoupon 结束 ===========");
        return 1;
    }

}